package com.bootdo.system.controller;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.domain.ItemDO;
import com.bootdo.system.service.ItemService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查项目控制器
 *
 * @author zhouxm
 */
@Controller
@RequestMapping("/system/item")
@Slf4j
public class ItemController extends BaseController {
    private String prefix = "system/item";

    @Autowired
    private ItemService itemService;

    @GetMapping()
    @RequiresPermissions("system:item:item")
    public String item() {
        return prefix + "/item";
    }

    @ApiOperation(value="获取检查项目列表", notes="")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:item:item")
    public List<ItemDO> list() {
        Map<String, Object> query = new HashMap<>(16);
        try {
            return itemService.list(query);
        } catch (Exception e) {
            log.error("出错：", e);
            return new ArrayList<>();
        }
    }

    @GetMapping("/add/{pId}")
    @RequiresPermissions("system:item:add")
    String add(@PathVariable("pId") Long pId, Model model) {
        model.addAttribute("pId", pId);
        String parentName = null;
        try {
            parentName = itemService.get(pId).getName();
        } catch (Exception e) {
            log.error("根据parentId获取项目出错：", e);
            parentName = "";
        }
        if (pId == 0) {
            model.addAttribute("pName", "总项目");
        } else {
            model.addAttribute("pName", parentName);
        }
        return  prefix + "/add";
    }

    @GetMapping("/edit/{itemId}")
    @RequiresPermissions("system:item:edit")
    String edit(@PathVariable("itemId") Long itemId, Model model) {
        Long parentId = null;
        ItemDO item = new ItemDO();
        try {
            item = itemService.get(itemId);
            parentId = item.getParentId();
        } catch (Exception e) {
            log.error("根据itemId获取项目出错：", e);
        }
        model.addAttribute("item", item);
        if(Constant.ITEM_ROOT_ID.equals(parentId)) {
            model.addAttribute("parentItemName", "无");
        }else {
            ItemDO parItem = new ItemDO();
            try {
                parItem = itemService.get(item.getParentId());
            } catch (Exception e) {
                log.error("根据parentId获取项目出错：", e);
            }
            model.addAttribute("parentItemName", parItem.getName());
        }
        return  prefix + "/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:item:add")
    public R save(ItemDO itemDO) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        try {
            if (itemService.save(itemDO) > 0) {
                return R.ok();
            }
        } catch (Exception e) {
            log.error("保存出错：", e);
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:item:edit")
    public R update(ItemDO itemDO) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        try {
            if (itemService.update(itemDO) > 0) {
                return R.ok();
            }
        } catch (Exception e) {
            log.error("修改出错：", e);
        }
        return R.error();
    }
    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:item:remove")
    public R remove(Long itemId) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("parentId", itemId);
        try {
            if(itemService.count(map)>0) {
                return R.error(1, "包含下级项目,不允许修改");
            }
            if(itemService.checkItemHasUser(itemId)) {
                if (itemService.remove(itemId) > 0) {
                    return R.ok();
                }
            }else {
                return R.error(1, "项目包含用户,不允许修改");
            }
        } catch (Exception e) {
            log.error("删除出错：", e);
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:item:batchRemove")
    public R remove(@RequestParam("itemIds[]") Long[] itemIds) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        try {
            itemService.batchRemove(itemIds);
        } catch (Exception e) {
            log.error("批量删除出错：", e);
        }
        return R.ok();
    }

    @GetMapping("/tree")
    @ResponseBody
    public Tree<ItemDO> tree() {
        Tree<ItemDO> tree = new Tree<>();
        try {
            tree = itemService.getTree();
        } catch (Exception e) {
            log.error("获取树出错：", e);
        }
        return tree;
    }

    @GetMapping("/treeView")
    String treeView() {
        return  prefix + "/deptTree";
    }
}
